<?php
	require_once 'environment.php';
	$temp->addSubMenu("Request a Prayer", "prayer.php?action=req");
	if($acc->isLogged()){
		$temp->addSubMenu("Moderate Prayers", "prayer.php?action=mod");
		$temp->addSubMenu("Delete Prayer", "prayer.php?action=del");
        $temp->addSubMenu("Silent Prayers", "prayer.php?action=silent");
	}
	
	$_prayer = new PrayerRequest($acc, $db);
	
	switch(fixArr($_GET, "action")){
		default:
			$temp->setTab(2);
			$temp->currentPage("Home >> Prayer");
			$temp->generate();
			$_prayer->listPrayerRequests();
			break;
		case("req"):
			$temp->setTab(2,0);
			$temp->currentPage("Home >> Prayer >> Request a Prayer");
			$temp->generate();
			if(!fixArr($_POST, "name")){
				//The basic form
				print "<form method=\"POST\">\n";
				print "<table width=60% align=center><tr><td style=\"width:40px\" colspan=\"2\"><h3>Prayer Request Form</h3><br /></td></tr>";
				print "<tr><td><label>Name: </label></td>";
				print "<td><input type=\"text\" size=40 maxlength=\"250\" name=\"name\" /></label></td></tr>";
				print "<tr><td><label>Email: </label></td><td><input type=\"text\" size=40 maxlength=\"250\" name=\"email\" /></td></tr>";
				print "<tr><td><label>Subject: </label></td><td><input type=\"text\" size=40 maxlength=\"50\" name=\"subj\" /></td></tr>";
				print "<tr><td valign=top>Message: </td>";
				print "<td><textarea name=\"request\" rows=\"10\" cols=\"40\"></textarea></td></tr>";
                print "<tr><td></td> <td><input type=\"checkbox\" name=\"silent\" /> Silent Prayer</td></tr>\n";
				print "<tr><td></td><td align=\"center\"><br /><input type=\"submit\" value=\"Submit\" /> <input type=\"reset\" value=\"Reset\" /></td></tr>";
				print "</table></form>";
			}else {
				$_prayer->submitPrayerRequest(fixArr($_POST, "name"), fixArr($_POST, "subj"), fixArr($_POST, "request"), fixArr($_POST, "email"), fixArr($_POST, "silent"));
				if(!fixArr($_POST, "silent")){
                    print "Your request has been added.<br />\nIt is now pending moderation. Please, come back every now and then to see if it was approved.";
                }else{
                    print "Your request has been added.<br />\nYou chose your prayer to be silent, so nobody but the Monster Ministries team will see it.";
                }
			}
			break;
		case("mod"):
			if(!$acc->isLogged()) die(change_site("prayer.php"));
			$temp->setTab(2,1);
			$temp->currentPage("Home >> Prayer >> Moderate Prayers");
			$temp->generate();
			//Get the unapproved prayer requests
			if(!fixArr($_GET, "approve")) {
				$sql = $db->Execute("SELECT * FROM `monster`.`prayers` WHERE `Approved` = '0' AND `Silent` = '0' ORDER BY `prayers`.`AutoInc` ASC");
				if($sql->RecordCount() > 0){
					while($row = $sql->FetchRow()) {
						print "<b>{$row['Subject']}</b><br />\n\"{$row['Message']}\" - {$row['Name']}<br />\n";
						print "<a href=\"prayer.php?action=mod&id={$row['AutoInc']}&approve=true\">Approve</a> - <a href=\"prayer.php?action=mod&id={$row['AutoInc']}&approve=false\">Deny</a><br />\n<br />\n";
					}
				}else{
					print "There are no pending prayer requests at this time.";
				}
			}else{
				$approve = fixArr($_GET, "approve");
				$id = fixArr($_GET, "id");
				if(!$id) die(change_site("prayer.php?action=mod"));
				if($approve === "true") {
					$_prayer->approvePrayerRequest($id);
					die(change_site("prayer.php?action=mod"));
				}else if($approve === "false") {
					$_prayer->declinePrayerRequest($id);
					die(change_site("prayer.php?action=mod"));
				}
			}
			break;
		case("del"):
			if(!$acc->isLogged()) die(change_site("prayer.php"));
			$temp->setTab(2,2);
			$temp->currentPage("Home >> Prayer >> Delete Prayer");
			$temp->generate();
			if(!fixArr($_GET, "id")) {
				$sql = $db->Execute("SELECT * FROM `monster`.`prayers` ORDER BY `prayers`.`AutoInc` ASC");
				while($row = $sql->FetchRow()) {
					print "<a href=\"prayer.php?action=del&id={$row['AutoInc']}\">{$row['Subject']}</a> - {$row['Name']}<br />\n";
				}
			}else{
				if(!fixArr($_GET, "approved")) {
					$id = fixArr($_GET, "id");
					print "Are you sure you want to delete this prayer request? It cannot be undone.<br />\n<br />\n";
					print "<a href=\"prayer.php?action=del&id={$id}&approved=true\">Yes, delete this prayer request.</a><br />\n<br />\n";
					print "<a href=\"prayer.php?action=del\">No, take me back to the delete page.</a><br />\n<br />\n";
				}else {
					if(fixArr($_GET, "approved") === "true") {
						$id = fixArr($_GET, "id");
						$_prayer->deletePrayerRequest($id);
						print "The prayer request was deleted successfully.";
					}else {
						die(change_site("prayer.php?action=del"));
					}
				}
			}
			break;
        case("silent"):
            if(!$acc->isLogged()) die(change_site("prayer.php"));
            $temp->setTab(2,3);
            $temp->currentPage("Home >> Prayer >> Silent Prayers");
            $temp->generate();
            //Get the unapproved prayer requests
            if(!fixArr($_GET, "approve")) {
                $sql = $db->Execute("SELECT * FROM `monster`.`prayers` WHERE `Silent` = '1' ORDER BY `prayers`.`AutoInc` ASC");
                if($sql->RecordCount() > 0){
                    while($row = $sql->FetchRow()) {
                        print "<b>{$row['Subject']}</b><br />\n\"{$row['Message']}\" - {$row['Name']}".($row['Email'] ? " - <a href=\"mailto:{$row['Email']}\">{$row['Email']}</a>" : "")."<br />\n";
                        print "<a href=\"prayer.php?action=mod&id={$row['AutoInc']}&approve=false\">Delete</a><br />\n<br />\n";
                    }
                }else{
                    print "There are no silent prayers at this time.";
                }
            }else{
                $approve = fixArr($_GET, "approve");
                $id = fixArr($_GET, "id");
                if(!$id) die(change_site("prayer.php?action=mod"));
                if($approve === "true") {
                    $_prayer->approvePrayerRequest($id);
                    die(change_site("prayer.php?action=silent"));
                }else if($approve === "false") {
                    $_prayer->declinePrayerRequest($id);
                    die(change_site("prayer.php?action=silent"));
                }
            }
            break;
	}
?>